package top.lyc829.iot_android.fragment.profile.friend;

import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gturedi.views.StatefulLayout;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xhttp2.request.CustomRequest;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import java.util.List;

import butterknife.BindView;
import top.lyc829.iot_android.R;
import top.lyc829.iot_android.adapter.FriendAdapter;
import top.lyc829.iot_android.adapter.LogAdapter;
import top.lyc829.iot_android.core.BaseFragment;
import top.lyc829.iot_android.entity.Log;
import top.lyc829.iot_android.entity.PageInfo;
import top.lyc829.iot_android.entity.User;
import top.lyc829.iot_android.http.service.LogService;
import top.lyc829.iot_android.http.service.UserService;
import top.lyc829.iot_android.http.subscriber.NoTipRequestSubscriber;

@Page(name = "FriendFragment")
public class FriendFragment extends BaseFragment {

    @BindView(R.id.friend_recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.ll_stateful)
    StatefulLayout mLlStateful;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    private FriendAdapter mFriendAdapter;

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle()
                .setTitle("室友");
        return titleBar;
    }

    public static FriendFragment newInstance() {
        return new FriendFragment();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.friend_fragment;
    }

    @Override
    protected void initViews() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mFriendAdapter = new FriendAdapter());
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    protected void initListeners() {
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final @NonNull RefreshLayout refreshLayout) {
                mLlStateful.showLoading();
                getFriendList(refreshLayout);
            }
        });
        mRefreshLayout.autoRefresh();
    }

    private void getFriendList(RefreshLayout refreshLayout) {

        CustomRequest request = XHttp.custom();

        request.apiCall(request.create(UserService.class).getFriends())
                .subscribeWith(new NoTipRequestSubscriber<List<User>>() {
                    @Override
                    protected void onSuccess(List<User> users) {
                        if (users == null || users.size() == 0) {
                            mLlStateful.showEmpty();
                        }
                        mLlStateful.showContent();
                        refreshLayout.finishRefresh(true);
                        mFriendAdapter.setList(users);
                    }

                    @Override
                    public void onError(ApiException e) {
                        refreshLayout.finishRefresh(true);
                        mLlStateful.showError(e.getMessage(), v -> {
                            getFriendList(refreshLayout);
                        });
                    }
                });

    }
}